package lib.pacificapp.util;
import org.json.JSONObject;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; 
/***
	 * 缓存操作
	 * @author rafe
	 * @version 1.0  
	 * */
public class ICookie {
 
	     private Context context=null;
		 /**键*/
		 private String key;
		 /**初始化
		  * @param mContext  
		  * @param mKey 自定义键
		  * */
	     public ICookie(Context mContext,String mKey){
	   	    context=mContext;
	   	    key=mKey;
	     }
	     /**获取操作对象*/
	    private SharedPreferences getSp(){
	    	 Context ctx =context;  
	    	  return ctx.getSharedPreferences(key,Context.MODE_PRIVATE);
	       }
	    /**获取 数字值*/
	    public int  number() {
	    	return number(false);
	    }
	    public int  number(boolean clear) {
	    	 SharedPreferences sp=getSp();
	    	 if(sp==null)return -1;
	    	 else {
			 int num= sp.getInt(key,-1);
			   if(clear)number(-1);
			   return num;
	    	 }
		 }
	    /**设置 数字值*/
	    public boolean  number(int number) {
			SharedPreferences sp=getSp();
			if(sp==null){
				return false;
				
			}else{
			Editor editor = getSp().edit();
			
	        editor.putInt(key, number);
	        editor.commit();
	        return true;
			}
		}
	    /**获取 字符串值*/
	    public String string() {
	    	return string(false);
	    }
	    /**获取 字符串值*/
	    public String string(boolean clear) {
			SharedPreferences sp=getSp();
	    	if(sp==null)return "error";
	    	 else {
	    		String data= getSp().getString(key,"");
	    		if(clear)string("");
	    		return data;
	    	 } 
		}
	    
	      /**设置 字符串值*/
	    public boolean  string(String string) {
			SharedPreferences sp=getSp();
			if(sp==null){
				return false;
				
			}else{
			    Editor editor = getSp().edit();
			    editor.putString(key, string);
			    editor.commit();
	         return true;
			}
		}
	    /**设置 JSON值*/
	    public boolean json(JSONObject obj) {
	   		  return   string(String.valueOf(obj));
	   	}
	    /**获取 JSON值*/
	     public JSONObject  json(){
	    	   String da=string();
	    	   JSONObject obj=new JSONObject();
	    	         try {
	   					 obj=new JSONObject(da);
	   					  } catch (Exception e) {
	   						obj=null;
	   					  }
	    	   return obj;
	     }
	}

